<template>
  <div class="system-user-container layout-padding">
    <el-card shadow="hover" class="layout-padding-auto">
      <div class="system-user-search mb15">
        <el-input
          size="default"
          v-model="state.tableData.param.mfdata"
          placeholder="请输入知识库名称"
          style="max-width: 180px"
        >
        </el-input>
        <el-select
          size="default"
          v-model="state.tableData.param.knowledge_id"
          placeholder="请选择所知识库"
          style="max-width: 180px"
        >
          <el-option
            v-for="item in knowledge_ids"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <el-button size="default" type="primary" class="ml10" @click="getTableData()">
          <el-icon>
            <ele-Search />
          </el-icon>
          查询
        </el-button>
        <el-button size="default" type="success" class="ml10" @click="onAddDoc">
          <el-icon>
            <ele-FolderAdd />
          </el-icon>
          新增文档
        </el-button>
      </div>
      <el-table
        :data="state.tableData.data"
        v-loading="state.tableData.loading"
        style="width: 100%"
      >
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column
          prop="create_time"
          label="创建时间"
          :formatter="formatterDate"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column prop="name" label="名称" show-overflow-tooltip></el-table-column>
        <el-table-column
          prop="kname"
          label="知识库"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="doc_type"
          label="文档类型"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="src_name"
          label="文件原始名"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="indexing_status"
          label="索引状态"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="tokens"
          label="tokens"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="操作" width="200">
          <el-button size="small" text type="primary" @click="onOpenEditDoc(scope.row)"
            >查看</el-button
          >
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="onHandleSizeChange"
        @current-change="onHandleCurrentChange"
        class="mt15"
        :pager-count="5"
        :page-sizes="[10, 20, 30]"
        v-model:current-page="state.tableData.param.pageNum"
        background
        v-model:page-size="state.tableData.param.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="state.tableData.total"
      >
      </el-pagination>
    </el-card>

    <DocumentDialog ref="docDialogRef" @refresh="getTableData()" />
  </div>
</template>
<script setup lang="ts">
import { defineAsyncComponent, reactive, onMounted, ref } from "vue";
import { ElMessageBox, ElMessage, dayjs } from "element-plus";
import { getAPI } from "/@/utils/axios-utils";
import { DocumentApi, ComDataApi } from "/@/api-services/api";
import { DocumentOut, ComDatasOut } from "/@/api-services/models";

const DocumentDialog = defineAsyncComponent(
  () => import("/@/views/document/docEdit.vue")
);

const docDialogRef = ref();
const knowledge_ids = ref<ComDatasOut[]>([]);

const state = reactive({
  tableData: {
    data: [] as Array<DocumentOut>,
    total: 0,
    loading: false,
    param: {
      mfdata: "",
      knowledge_id: "",
      pageNum: 1,
      pageSize: 10,
    },
  },
});

const getTableData = async () => {
  state.tableData.loading = true;
  var res = await getAPI(DocumentApi).docList(state.tableData.param);

  state.tableData.data = res.data.data ?? [];
  state.tableData.total = res.data.count;
  setTimeout(() => {
    state.tableData.loading = false;
  }, 500);
};

const onOpenEditDoc = (row: DocumentOut) => {
  docDialogRef.value.openDialog(row);
};

const onAddDoc = () => {
  // 打开上传文件窗口
  docDialogRef.value.openDialog({});
};

// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};

const getComData = async () => {
  const res = await getAPI(ComDataApi).getKnowledgeList({});
  knowledge_ids.value.splice(0);
  res.data.data.forEach((item: any) => {
    knowledge_ids.value.push({
      id: item.id,
      name: item.name,
    });
  });
};

// 页面加载时
onMounted(() => {
  getComData();
  getTableData();
});

const formatterDate = (row: any, column: any) => {
  return dayjs(row.create_time * 1000).format("YYYY-MM-DD HH:mm:ss");
};
</script>

<style scoped lang="scss">
.system-user-container {
  :deep(.el-card__body) {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: auto;
    .el-table {
      flex: 1;
    }
  }
}
</style>
